﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;

using ProjectBase.Core;
using ProjectBase.Core.Web;
using ProjectBase.Mvc;

using Survey.Entity;
using Survey.Common.Models;
using Survey.Common;

namespace Survey.Web.Controls
{
    public class AnswerTable : ContentControl<IQuestionModel, AnswerTable>
    {
        public AnswerTable(string cssClassName = null) : base(cssClassName ?? "w48p mh100") { }
        
        protected override void BuildControls()
        {
            Controls.Add(new Fieldset(GetAttribute(Attr.Class), "Answers")
            {
                new Table("edit list")
                {
                    CreateHead(),
                    CreateBody(),
                    Model.IsActionDetail() ? null : CreateExtenders(),
                    Model.IsActionDetail() ? null : CreateTemplate(),
                }
            });
        }

        protected virtual TableHead CreateHead()
        {
            return new TableHead { new TableRow
            {
                new TableHeaderCell ("w40") { new Literal ("Position", true), },
                new TableHeaderCell ("w50p") { new Literal ("Title", true), },
                new TableHeaderCell ("w50p") { new Literal ("Description", true), },
                Model.IsActionDetail() ? null
                : new TableHeaderCell ("w50") { new Literal ("Remove", true), },
            }};
        }

        protected virtual TableBody CreateBody()
        {
            // rows for existing Answer
            var tbody = new TableBody { new TableRow("hidden") { new TableCell(colspan: 3) { new Literal(Special.Space) } } };
            foreach (var answer in Model.Item.Answers)
            {
                tbody.AddControls(new AnswerRow
                {
                    Item = answer,
                    Prefix = Prefix,
                });
            }
            if (!Model.IsActionDetail())
            {
                for (var i = Model.Item.Answers.Count; i < 3; i++)
                {
                    tbody.AddControls(new AnswerRow
                    {
                        Item = new Answer(),
                        Index = i.ToString(),
                        Prefix = Prefix,
                    });
                }
            }
            return tbody;
        }

        protected virtual TableBody CreateTemplate()
        {
            return new TableBody("hidden")
            {
                new AnswerRow
                {
                    Item = new Answer(),
                    Index = "{{index}}",
                    Prefix = Prefix,
                }
            }
            .SetId("AnswerTemplate");
        }

        protected virtual TableBody CreateExtenders()
        {
            // empty TR as delimiter
            var tbExtenders = new TableBody
            {
                new TableRow
                {
                    new TableCell(colspan: Model.IsActionDetail() ? 3 : 4)
                    {
                        new Literal(Special.Space)
                    }
                }
            };

            // row with the add button
            tbExtenders.AddControls(new TableRow
            {
                new TableCell("Add w40", colspan: 2)
                {
                    new Span { new Literal ("AddAnswer", true), },
                },
                new TableCell("Add w100", colspan: 2)
                {
                    new Button("Add").SetName("AddNewCollItem")
                        .SetAttribute("data-prefix", Prefix)
                        .SetAttribute("data-templateid", "AnswerTemplate"),
                },
            });
            return tbExtenders;
        }

        public virtual string Prefix { get; set; }
    }
}